﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;

namespace NJCORS.forms
{
    public partial class ManageLandInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
            diverrormsg.Style.Add("display", "none");
        }

        protected void BindData()
        {
            string strsql = @"select a.LandID, a.AttributeID, a.LandName, a.LandScope,
                                     b.AttributeName
                              from LandInfo a, AttributeInfo b 
                              where a.AttributeID = b.AttributeID and a.Enabled = 1";
            DataSet ds = DBUtility.DBHelperSQL.GetDataSet(strsql);
            griddata.DataSource = ds;
            griddata.DataKeyNames = new string[] { "LandID"};
            griddata.DataBind();
            GlobalMethods.SetGridViewWidth(griddata, true);

            if (ds.Tables[0].Rows.Count < 1)
            {
                divpager.Style.Add("display", "none");
            }
            else
            {
                LabelRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
                LabelCurrentPage.Text = (griddata.PageIndex + 1).ToString();
                LabelPageCount.Text = griddata.PageCount.ToString();
                divpager.Style.Add("display", "block");
            }
        }

        protected void griddata_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int cnt = -1;
                string strLandName = ((TextBox)(griddata.Rows[e.RowIndex].FindControl("txbLandName"))).Text.ToString().Trim();
                if (strLandName.Length < 1)
                {
                    throw new Exception("请输入土地名称！");
                }
                string strLandScope = ((TextBox)(griddata.Rows[e.RowIndex].FindControl("txtLandScope"))).Text.ToString().Trim();
                if (strLandScope.Length < 1)
                {
                    throw new Exception("请输入土地范围！");
                }

                cnt = Convert.ToInt32(griddata.DataKeys[e.RowIndex].Values[0]);
                if (cnt > 0)
                {
                    Model.LandInfo mrui = new Model.LandInfo();
                    BLL.LandInfo brui = new BLL.LandInfo();
                    mrui = brui.GetModel(cnt);
                    if (mrui.LandName != strLandName)
                    {
                        //DataSet ds = brui.GetList("Enabled=1 and LandName='" + strLandName + "'");
                        if (brui.Exists(strLandName))//(ds.Tables[0].Rows.Count > 0)//
                        {
                            throw new Exception("土地名称已存在!");
                        }
                        else
                        {
                            mrui.LandName = strLandName;
                        }
                    }
                    mrui.LandScope = strLandScope;
                    mrui.AttributeID = int.Parse(((DropDownList)(griddata.Rows[e.RowIndex].FindControl("ddlAttributeID"))).SelectedValue);
                    brui.Update(mrui);

                    griddata.EditIndex = -1;
                    BindData();
                }
            }
            catch (System.Exception ex)
            {
                griddata.EditIndex = -1;
                BindData();
                ShowMessage(false, ex.Message);
            }
        }

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int cnt = -1;
                cnt = Convert.ToInt32(griddata.DataKeys[e.RowIndex].Values[0]);
                if (cnt > 0)
                {
                    string strsql = "update LandInfo set Enabled=0 where LandID=" + cnt.ToString();
                    DBUtility.DBHelperSQL.GetNums(strsql);
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(false, ex.Message);
            }
        }

        protected void griddata_RowEditing(object sender, GridViewEditEventArgs e)
        {
            griddata.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                BLL.AttributeInfo bai = new BLL.AttributeInfo();
                DataSet ds = new DataSet();
                ds = bai.GetList("Enabled = 1");
                DropDownList ddlai = (DropDownList)e.Row.FindControl("ddlAttributeID");
                if (ddlai != null)
                {
                    ddlai.DataSource = ds;
                    ddlai.DataValueField = "AttributeID";
                    ddlai.DataTextField = "AttributeName";
                    ddlai.DataBind();
                    ddlai.SelectedValue = ((HiddenField)e.Row.FindControl("hfAttributeID")).Value;
                }

                e.Row.Attributes.Add("onclick", "click2(this)");

                //如果是绑定数据行 
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.FindControl("btndelete")).Attributes.Add("onclick",
                        "javascript:return confirm('你确认要土地\"" + ((Label)e.Row.FindControl("lbLandName")).Text + "\"吗?')");
                }
            }
        }

        protected void griddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            griddata.EditIndex = -1;
            BindData();
        }

        protected void ShowMessage(bool issuccessful, string message)
        {
            diverrormsg.Style.Add("display", "block");
            if (issuccessful == true)
            {
                diverrormsg.Style.Add("background-color", "#CCFF99");
                diverrormsg.Style.Add("border-color", "#009900");
                diverrormsg.Style.Add("color", "#006600");
                diverrormsg.Style.Add("background-image", "url(../themes/images/successful.gif)");
                lberrormsg.Text = message;
            }
            else
            {
                diverrormsg.Style.Add("background-color", "#ffebe8");
                diverrormsg.Style.Add("border-color", "#dd3c10");
                diverrormsg.Style.Add("color", "#dd3c10");
                diverrormsg.Style.Add("background-image", "url(../themes/images/error.gif)");
                lberrormsg.Text = message;
            }
        }

        protected void btnquery_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }

        protected void PagerButton_Click(object sender, EventArgs e)
        {
            int pageIndx = Convert.ToInt32(LabelCurrentPage.Text);
            pageIndx = pageIndx - 1;
            int pages = Convert.ToInt32(LabelPageCount.Text);
            string arg = ((ImageButton)sender).CommandArgument.ToString().ToLower();
            switch (arg)
            {
                case "prev":
                    if (pageIndx > 0)
                    {
                        pageIndx -= 1;
                    }
                    break;
                case "next":
                    if (pageIndx < pages - 1)
                    {
                        pageIndx += 1;
                    }
                    break;
                case "last":
                    pageIndx = pages - 1;
                    break;
                default:
                    pageIndx = 0;
                    break;
            }
            griddata.PageIndex = pageIndx;
            pageIndx = pageIndx + 1;
            LabelCurrentPage.Text = pageIndx.ToString();
            BindData();
        }

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex < 0)
            {
                return;
            }
            if (e.NewPageIndex > griddata.PageCount)
            {
                return;
            }
            griddata.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void btngo_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                int pageIndx = Convert.ToInt32(LabelCurrentPage.Text);
                pageIndx = pageIndx - 1;
                int pages = Convert.ToInt32(LabelPageCount.Text);
                if (txtpagnum.Text.Length < 1)
                {
                    throw new Exception("请输入目标页码！");
                }
                string numregex = @"^\d+$";
                if (!Regex.IsMatch(txtpagnum.Text, numregex))
                {
                    throw new Exception("目标页码格式错误！");
                }
                int targetpagenum = Convert.ToInt32(txtpagnum.Text);
                if (targetpagenum > pages)
                {
                    pageIndx = pages - 1;
                }
                else if (targetpagenum < 1)
                {
                    pageIndx = 0;
                }
                else
                {
                    pageIndx = targetpagenum - 1;
                }
                griddata.PageIndex = pageIndx;
                pageIndx = pageIndx + 1;
                LabelCurrentPage.Text = pageIndx.ToString();
                BindData();
            }
            catch (Exception ex)
            {
                ShowMessage(false, ex.Message);
            }
        }
    }
}
